/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blink.view.task;

import blink.model.Stimulus;
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.WeakHashMap;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;

/**
 *
 * @author jerome
 */
public class StimulusListCellRenderer extends DefaultListCellRenderer {
    
    private static int HEIGHT=100;
    private Map<Object,ImageIcon> cache = new WeakHashMap<Object,ImageIcon>();
    
    public Component getListCellRendererComponent(
                                       JList list,
                                       Object value,
                                       int index,
                                       boolean isSelected,
                                       boolean cellHasFocus) {
        
            Stimulus sti = (Stimulus) value;
            super.getListCellRendererComponent(list, "", TOP, isSelected, isSelected);
            
            this.setHorizontalAlignment(CENTER);
            this.setIcon(getIcon(sti));
            return this;
        }
    
    private ImageIcon getIcon(Stimulus sti) {
        ImageIcon icon = cache.get(sti);
        if (icon==null) {
            Image img = sti.getImg(-1,HEIGHT);
            icon = new ImageIcon(img);
            cache.put(sti, icon);
        }
        return icon;
    }
}
